package com.fz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fz.entity.Log;
import com.fz.service.LogService;
import com.fz.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 * 存储用户所有操作日志 前端控制器
 * </p>
 *
 * @author hyr
 * @since 2025-06-18
 */
@RestController
@RequestMapping("/log")
public class LogController {
    @Resource
    LogService logService;
    @Autowired
    private ObjectMapper objectMapper; // 用于解析JSON字符串
    @PostMapping("/list")
    ResultVo<IPage<Log>> list(Long userId, Integer PageNo, Integer PageSize, String filtersJson, String sortJson, String orderNumber) throws InterruptedException, JsonProcessingException
    {
        return ResultVo.success(logService.userpage(userId, PageNo, PageSize, objectMapper.readValue(filtersJson, Map.class), objectMapper.readValue(sortJson, Map.class),orderNumber));
    }
    @GetMapping("/AdminList")
    public ResultVo<IPage<Log>> Adminlist(Integer PageNo, Integer PageSize) throws InterruptedException {
        Page<Log> page = new Page<>(PageNo, PageSize);
        IPage<Log> resultPage = logService.page(page, new QueryWrapper<Log>().in("user_id",  2L));
        return ResultVo.success(resultPage);
    }
    @GetMapping("/UserList")
    public ResultVo<IPage<Log>> Userlist(Integer PageNo, Integer PageSize) throws InterruptedException {
        Page<Log> page = new Page<>(PageNo, PageSize);
        IPage<Log> resultPage = logService.page(page, new QueryWrapper<Log>().in("user_id",  1L));
        return ResultVo.success(resultPage);
    }
}
